/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components.savedplaylist;

import com.bff.bjj.ui.component.MusicList;
import com.bff.bjj.ui.component.MusicListCellRenderer;
import com.bff.slimserver.music.MusicObjects;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import java.awt.Component;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.border.EmptyBorder;
import org.bff.slimserver.SlimSavedPlaylistManager;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.musicobjects.SlimSavedPlaylist;

/**
 *
 * @author Bill
 */
public class SavedPlaylistList extends MusicList {

    private List<SlimSavedPlaylist> playlists;
    private SlimSavedPlaylistManager playlistManager;
    private DefaultListModel playlistListModel;

    public SavedPlaylistList() {
        playlists = new ArrayList<SlimSavedPlaylist>();
        playlistListModel = new DefaultListModel();
        try {
            setPlaylistManager(JukeboxProperties.getInstance().getPlaylistManager());
        } catch (SlimConnectionException ex) {
            Logger.getLogger(SavedPlaylistList.class.getName()).log(Level.SEVERE, null, ex);
        }

        listAll();

        setCellRenderer(new MenuListCellRenderer());
    }

    public SlimSavedPlaylist getSelectedPlaylist() {
        if (getSelectedValue() == null) {
            return null;
        }

        return (SlimSavedPlaylist) getSelectedValue();
    }

    public void refresh() {
        listAll();
    }

    private void listAll() {
        setPlaylists(new ArrayList<SlimSavedPlaylist>(MusicObjects.getSavedPlaylists()));

        loadList();
    }

    public int getCount() {
        return getPlaylists().size();
    }

    private void loadList() {
        try {
            playlistListModel.clear();

            for (SlimSavedPlaylist pl : getPlaylists()) {
                playlistListModel.addElement(pl);
            }

            setModel(playlistListModel);
            setSelectedIndex(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @return the playlistManager
     */
    public SlimSavedPlaylistManager getPlaylistManager() {
        return playlistManager;
    }

    /**
     * @param playlistManager the playlistManager to set
     */
    public void setPlaylistManager(SlimSavedPlaylistManager playlistManager) {
        this.playlistManager = playlistManager;
    }

    /**
     * @return the playlists
     */
    private List<SlimSavedPlaylist> getPlaylists() {
        return playlists;
    }

    /**
     * @param playlists the playlists to set
     */
    private void setPlaylists(List<SlimSavedPlaylist> playlists) {
        this.playlists = playlists;
    }

    private class MenuListCellRenderer extends MusicListCellRenderer {

        public MenuListCellRenderer() {
            setOpaque(true);
        }

        @Override
        public Component getListCellRendererComponent(JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean isFocused) {

            if (value instanceof SlimSavedPlaylist) {
                setText(((SlimSavedPlaylist) value).getName());
                setBorder(new EmptyBorder(new Insets(2, 2, 2, 2)));
            }

            setColors(list, index, isSelected);

            return (this);
        }
    }
}
